package cn.jn.lhm.teacher.handler.front;

import cn.jn.lhm.teacher.entity.EduCourse;
import cn.jn.lhm.teacher.entity.EduTeacher;
import cn.jn.lhm.teacher.service.EduCourseService;
import cn.jn.lhm.teacher.service.EduTeacherService;
import cn.jn.lhm.utils.R;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * @author LHM
 * @Tips 热爱每一天，奋斗每一天
 * @date 2020/6/9 - 19:59
 */
@RestController
@RequestMapping("/teacher/front")
public class IndexTeacherHandler {

    @Resource
    private EduTeacherService eduTeacherService;

    @Resource
    private EduCourseService eduCourseService;

    @GetMapping("/get/hot")
    public R getHotCourseAndTeacher(){
        //查询teacher
        QueryWrapper<EduTeacher> teacherQueryWrapper = new QueryWrapper<>();
        teacherQueryWrapper.orderByDesc("sort");
        teacherQueryWrapper.last("limit 4");
        List<EduTeacher> eduTeacherList = eduTeacherService.listByWrap(teacherQueryWrapper);
        //查询热门课程
        QueryWrapper<EduCourse> courseQueryWrapper = new QueryWrapper<>();
        courseQueryWrapper.orderByDesc("view_count");
        courseQueryWrapper.last("limit 8");
        List<EduCourse> courseList = eduCourseService.listByWrap(courseQueryWrapper);

        return R.ok().data("teacherList",eduTeacherList).data("courseList",courseList);
    }

    @GetMapping("/get/teacher/info/{id}")
    public R getTeacherInfoById(@PathVariable("id")String id){
        //需要查询讲师的信息以及对应的课程信息
        EduTeacher teacher = eduTeacherService.getTeacherInfo(id);
        List<EduCourse> courseList = eduCourseService.getCourseInfoByTeacherId(id);
        return R.ok().data("teacher", teacher).data("courseList", courseList);
    }

    @PostMapping("/get/teacher/page/{current}/{limit}")
    public R getTeacherListByPage(@PathVariable("current")Long current,
                                  @PathVariable("limit")Long size){
        Page<EduTeacher> page = new Page<>(current, size);
        Map<String, Object> map = eduTeacherService.listByPageToMap(page);
        return R.ok().data(map);
    }


}
